package com.wangyu.talents.web.system.user.controller;

import com.google.common.collect.Maps;
import com.wangyu.talents.common.utils.JWTUtil;
import com.wangyu.talents.core.entity.Page;
import com.wangyu.talents.core.entity.ResponseModel;
import com.wangyu.talents.core.web.controller.BaseController;
import com.wangyu.talents.web.system.user.model.User;
import com.wangyu.talents.web.system.user.service.UserService;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统用户控制器
 *
 * @author wangyu
 * @Date 2018-08-31 17:23
 */
@RestController
@RequestMapping("/v1/user")
public class UserController extends BaseController {

  @Autowired
  private UserService userService;

  /**
   * 新增用户
   *
   * @author wangyu
   * @Date 2018/9/3 18:04
   */
  @RequiresRoles(value = "admin")
  @RequestMapping(value = "/", method = RequestMethod.POST)
  public ResponseModel save(@RequestBody User user) {
    int insert = userService.insert(user);
    return ResponseModel.newOk("成功插入" + insert + "条数据");
  }

  /**
   * 修改用户
   *
   * @author wangyu
   * @Date 2018/9/3 18:04
   */
  @RequestMapping(value = "/", method = RequestMethod.PATCH)
  public ResponseModel update(@RequestBody User user) {
    int update = userService.update(user);
    return ResponseModel.newOk("成功更新" + update + "条数据");
  }

  /**
   * 删除用户（真删除）
   *
   * @author wangyu
   * @Date 2018/9/3 18:05
   */
  @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  public ResponseModel delete(@PathVariable("id") Long id) {
    int delete = userService.delete(id);
    return ResponseModel.newOk("成功删除" + delete + "条数据");
  }

  /**
   * 分页查询用户列表
   *
   * @author wangyu
   * @Date 2018/9/3 18:05
   */
  @RequestMapping(value = "/findCondition", method = RequestMethod.POST)
  public ResponseModel findCondition(@RequestBody Page<User> page) {
    page = userService.findList(page);
    return ResponseModel.newOk(page);
  }

  /**
   * 查询用户详情
   *
   * @author wangyu
   * @Date 2018/9/3 18:05
   */
  @RequestMapping(value = "/{userId}", method = RequestMethod.GET)
  public ResponseModel getDetails(@PathVariable("userId") Long userId) {
    User user = userService.getDetails(userId);
    return ResponseModel.newOk(user);
  }

  /**
   * 查询用户详情
   *
   * @author wangyu
   * @Date 2018/9/3 18:05
   */
  @RequestMapping(value = "/toList", method = RequestMethod.GET)
  public ResponseModel toList() {
    Map<String, Object> map = Maps.newHashMap();
    List<User> list = userService.findList(map);
    return ResponseModel.newOk(list);
  }

  /**
   * 用户登录
   */
  @RequestMapping(value = "/login", method = RequestMethod.POST)
  public ResponseModel login(@RequestBody User user) {
    User currentUser = userService.login(user.getLoginName(), user.getPassword());
    return ResponseModel.newOk(JWTUtil.createToken(currentUser.getLoginName()));
  }

  @RequestMapping(path = "/unauthorized/{message}")
  public ResponseModel unauthorized(@PathVariable String message)
      throws UnsupportedEncodingException {
    return ResponseModel.newError(message);
  }
}
